package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {

    /**
     * 反射获取对象
     */
    public static void reflectNewInstance() {

        Class<?> c3 = null;
        try {
            c3 = Class.forName("reflectdemo.Student");
            Student student = (Student)c3.newInstance();// 通过.class，创建类的实例
            System.out.println(student);
            student.sleep();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        Class<?> c3 = null;
        try {
            c3 = Class.forName("reflectdemo.Student");
            Constructor<?> constructor =
                    c3.getDeclaredConstructor(String.class, int.class);//获得该类中与参数类型匹配的构造方法

            //设置为true后可修改访问权限
            constructor.setAccessible(true);

            Student student = (Student) constructor.newInstance("frost", 19);

            System.out.println(student);

        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有属性
    public static void reflectPrivateField() {
        Class<?> c3 = null;
        try {
            c3 = Class.forName("reflectdemo.Student");
            Student student = (Student) c3.newInstance();

            Field field = c3.getDeclaredField("name");
            field.setAccessible(true);

            field.set(student, "caocao");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    // 反射私有方法
    public static void reflectPrivateMethod() {
        Class<?> c3 = null;
        try {
            c3 = Class.forName("reflectdemo.Student");
            Student student = (Student)c3.newInstance();// 通过.class，创建类的实例

            Method method = c3.getDeclaredMethod("function", String.class);

            method.setAccessible(true);

            method.invoke(student, "参数");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}
